package impl.javasistimpl;

import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;

/**
 * Created by chenjun on 2017/11/22.
 */
public class JavassistProxyGenerator {
    public static  Object newProxyInstance(Class<?>[] interfaces, MethodHandler handler) {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setInterfaces(interfaces);
        Class<?> proxyClass = proxyFactory.createClass();
        Object javassistProxy = null;
        try {
            javassistProxy = proxyClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        ((ProxyObject) javassistProxy).setHandler(handler);
        return javassistProxy;
    }
}
